package com.yanqu.road.dao.impl.cross.banquet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.cross.CrossBanquetData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBanquetDaoImpl extends TempDataObjectDao<CrossBanquetData> {

    public Map<String, CrossBanquetData> getRunningCrossBanquetMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        //假如在开宴会的时候出现异常了，重启了就不管了 这里改了CrossBanquetMemberDaoImpl这个也要改一下啊
        String sql = "select * from t_c_banquet where (isSendReward = 0 AND status = 1) or (openTime > ? and status = 0)";
        Map<String, CrossBanquetData> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, System.currentTimeMillis() - 10*60*10000);

            rs = call.executeQuery();
            while (rs.next()) {
                CrossBanquetData data = parse(rs);
                res.put(data.getBanquetId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }

    @Override
    protected boolean addData(CrossBanquetData data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_c_banquet(serverId,userId,banquetId,type,openTime,overTime,isSendReward," +
                "popularity,integral,userCount,userBaseInfo,serverList,invitationId,invitationInfo,status,giftTypeAccept,message)" +
                " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getServerId());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getBanquetId());
            call.setInt(i++, data.getType());
            call.setLong(i++, data.getOpenTime());
            call.setLong(i++, data.getOverTime());
            call.setBoolean(i++, data.isSendReward());
            call.setLong(i++, data.getPopularity());
            call.setLong(i++, data.getIntegral());
            call.setInt(i++, data.getUserCount());
            call.setString(i++, data.getUserBaseInfoJsonStr());
            call.setString(i++, data.getServerListStr());
            call.setString(i++, data.getInvitationId());
            call.setString(i++, data.getInvitationInfo());
            call.setInt(i++, data.getStatus());
            call.setString(i++, data.getGiftTypeAccept());
            call.setString(i++, data.getMessage());

            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(CrossBanquetData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_c_banquet SET isSendReward=?,popularity=?,integral=?,userCount=?,status=?,giftTypeAccept=?,message=? where banquetId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setBoolean(i++, data.isSendReward());
            call.setLong(i++, data.getPopularity());
            call.setLong(i++, data.getIntegral());
            call.setLong(i++, data.getUserCount());
            call.setInt(i++, data.getStatus());
            call.setString(i++, data.getGiftTypeAccept());
            call.setString(i++, data.getMessage());

            call.setString(i++, data.getBanquetId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected CrossBanquetData parseData(ResultSet rs) throws SQLException {
        CrossBanquetData data = new CrossBanquetData();
        data.setBanquetId(rs.getString("banquetId"));
        data.setUserId(rs.getLong("userId"));
        data.setServerId(rs.getInt("serverId"));
        data.setType(rs.getInt("type"));
        data.setOpenTime(rs.getLong("openTime"));
        data.setOverTime(rs.getLong("overTime"));
        data.setSendReward(rs.getBoolean("isSendReward"));
        data.setPopularity(rs.getLong("popularity"));
        data.setIntegral(rs.getLong("integral"));
        data.setUserCount(rs.getInt("userCount"));
        data.setServerListStr(rs.getString("serverList"));
        data.setInvitationId(rs.getString("invitationId"));
        data.setInvitationInfo(rs.getString("invitationInfo"));
        data.setStatus(rs.getInt("status"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo){
            data.setUserBaseInfo(userBaseInfo);
        }
        data.setGiftTypeAccept(rs.getString("giftTypeAccept"));
        data.setMessage(rs.getString("message"));
        if (data.getMessage() == null) {
            data.setMessage("");
        }
        return data;
    }
}
